package com.laomo.lib_draw_text;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.Log;

import java.util.Objects;

/**
 * 类说明：
 *
 * @author laomo
 * @date 2024/8/9 09:01
 */
public class DrawTextBean {
    String mId;
    private Context mContext;
    int mWidth;
    int mHeight;
    Bitmap mBitmap;
    Canvas mCanvas;
    Paint mPaint;

    public DrawTextBean(Context context, int width, int height, int textHeight) {
        mContext = context;
        //Log.d("lkjdflsd", "DrawTextBean "+width+" ,"+height);
        mWidth = width;
        mHeight = height;
        mId = mWidth + String.valueOf(mHeight);
        //initCanvas(width, height);

        mPaint = new TextPaint();
        mPaint.setTextSize(textHeight);
        initTextPaintStyle();
        mPaint.setDither(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setAntiAlias(false);

    }

    private void initCanvas(int width, int height) {
        if (mBitmap != null) {
            if (mBitmap.getWidth() != width || mBitmap.getHeight() != height) {
                mBitmap.recycle();
                mBitmap = null;
            } else {
                return;
            }
        }
        mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }


    @SuppressLint("WrongConstant")
    public void initTextPaintStyle() {
        int style = DrawTextUtil.getStyle();
        String fontFilePath = DrawTextUtil.getFontFilePath();
        if (TextUtils.isEmpty(fontFilePath)) {
            mPaint.setTypeface(Typeface.create(Typeface.DEFAULT, style));
        } else {
            mPaint.setTypeface(Typeface.createFromFile(fontFilePath));
        }
    }


    public Bitmap getTextBitmap(String str, int textHeight) {
        Rect rect = new Rect();
        mPaint.setTextSize(textHeight);
        mPaint.getTextBounds(str, 0, str.length(), rect);
        Log.d("lkjdflsd", "rect:" + rect);
        Log.d("lkjdflsd", "rect:" + rect.width() + "," + rect.height());

        if (mWidth == -1) {
            initCanvas(rect.right + Math.abs(rect.left), mHeight);
        } else {
            initCanvas(mWidth, mHeight);
        }
        Log.d("lkjdflsd", "mBitmap:" + mBitmap.getWidth() + ", " + mBitmap.getHeight());

        //清空画布
        mCanvas.drawColor(Color.BLACK);
        //Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
        //Log.d("lkjdflsd", "ascent:" + fontMetrics.ascent + "descent:" + fontMetrics.descent + "leading:" + fontMetrics.leading);
        //Log.d("lkjdflsd", "text height:" + (fontMetrics.descent - fontMetrics.ascent));

        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
        float baseLine = mHeight / 2.0f - (fontMetrics.descent + fontMetrics.ascent) / 2;


        //float baseLine = rect.bottom;
        /*if (baseLine + rect.bottom < mHeight) {
            baseLine += (int) ((mHeight - baseLine) / 2);
        }*/

        float startX = 0;
        //if (rect.right < mWidth) {
        //startX = (width - rect.right) / 2;
        //}
        mCanvas.drawText(str, startX, baseLine, mPaint);
        return mBitmap;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DrawTextBean that = (DrawTextBean) o;
        return Objects.equals(mId, that.mId);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(mId);
    }
}
